#include "bufferBlock.hpp"
using namespace wheels;
using namespace dm;

#define BUFFER_SIZE     (1024 * 1024 * 50 )

IMP_SINGLETON( buff );

buff::buff():__pt_data( BUFFER_SIZE ),__m_curr_pos(0) , __m_size( BUFFER_SIZE ) {}

bool buff::toString( std::string& rst )
{
    bool ret = true;

    if( __pt_data.dataLen() <= 0 ) return false;

    rst.assign( __pt_data.get() , __pt_data.dataLen() );
    return ret;
}

bool buff::write( const char * data , size_t len , bool clearPos )
{
    bool ret = false;

    if( clearPos ){
        __m_curr_pos = 0;
    }

    if( len + __m_curr_pos < __m_size ){
        memcpy( __pt_data.get() + __m_curr_pos , data , len );
        __m_curr_pos += len;
        return true;
    }

    ERROR_MSG( "写入数据过长" );
    return ret;
}
bool buff::write( const std::string& data , bool clearPos )
{
    bool ret = false;
    if( clearPos ){
        __m_curr_pos = 0;
    }

    if( data.length() + __m_curr_pos < __m_size ){
        memcpy( __pt_data.get() + __m_curr_pos , data.c_str() , data.length() );
        __m_curr_pos += data.length();
        return true;
    }

    ERROR_MSG( "写入数据过长" );
    return ret;
}
